/**
 * @Description: 定位基础类
 * @Author: 胡凯
 * @CreateDate: 2020-08-10
 * @Version: 1.0
 */
package com.p595263720.o_amap.location;


import android.content.Context;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.CoordinateConverter;
import com.amap.api.location.DPoint;
import com.google.gson.Gson;
import com.p595263720.o_amap.OAmapPlugin;
import com.p595263720.o_amap.location.model.UnifiedLocationClientOptions;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

public class LocationHandlers implements MethodChannel.MethodCallHandler, AMapLocationListener {

    private AMapLocationClient mAapLocationClient;
    private MethodChannel.Result result;
    private EventChannel.EventSink eventSink; //发射器
    private Gson gson;
    private Context mContext;


    public LocationHandlers(BinaryMessenger messenger,Context context) {
        mContext = context;
        gson = new Gson();
        // 初始化定位
        initLocation();
        //持续通道
        EventChannel eventChannel = new EventChannel(messenger, "com.p595263720.zit/location_receive");
        EventChannel.StreamHandler streamHandler = new EventChannel.StreamHandler() {
            @Override
            public void onListen(Object arguments, EventChannel.EventSink events) {
                eventSink = events;
            }

            @Override
            public void onCancel(Object arguments) {
                eventSink = null;
            }
        };
        eventChannel.setStreamHandler(streamHandler);
    }

    @Override
    public void onMethodCall(MethodCall call, MethodChannel.Result result) {
        this.result = result;
        invokeMethod(call);
    }

    private void invokeMethod(MethodCall call) {
        switch (call.method) {
            case "startLocation": // 开启定位
                startLocation(call);
                break;
            case "stopLocation": // 停止定位
                stopLocation();
                break;
            case "destroyLocation": // 销毁
                destroyLocation();
                break;
            case "coordinateTransformation": // 销毁
                coordinateTransformation(call);
                break;
            case "calculateLineDistance": // 销毁
                calculateLineDistance(call);
                break;
        }
    }


    //初始化定位
    public void initLocation() {
        if (mAapLocationClient == null) {
            mAapLocationClient = new AMapLocationClient(mContext);
        }
        mAapLocationClient.setLocationListener(this);
    }

    //开启定位
    public void startLocation(MethodCall call) {
        String optionJson = call.argument("options") != null ? (String) call.argument("options") : "{}";
        AMapLocationClientOption option = gson.fromJson(optionJson, UnifiedLocationClientOptions.class).toLocationClientOptions();
        if (null != mAapLocationClient) {
            stopLocation();
            mAapLocationClient.setLocationOption(option);
            mAapLocationClient.startLocation();
        }
    }

    //结束定位
    public void stopLocation() {
        if (null != mAapLocationClient) {
            mAapLocationClient.stopLocation();
        }
//        if(null!=eventSink){
//            eventSink.endOfStream();
//        }
    }

    // 销毁定位
    public void destroyLocation() {
        if (null != mAapLocationClient) {
            mAapLocationClient.onDestroy();
        }
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (aMapLocation != null) {
            eventSink.success(JSON.toJSONString(aMapLocation));
        } else {
            Toast.makeText(mContext, "定位失败!", Toast.LENGTH_LONG).show();
        }
    }


    // 执行坐标转换并返回
    public void coordinateTransformation(MethodCall call) {
        try {
            CoordinateConverter converter = new CoordinateConverter(mContext);
            int coordinateType = call.argument("coordinateType");
            setCoordinateType(converter, coordinateType);
            converter.coord(new DPoint((Double)call.argument("longitude"), (Double)call.argument("latitude")));
            // 执行转换操作
            DPoint desLatLng = converter.convert();
            this.result.success(JSON.toJSONString(desLatLng));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 判断传入坐标类型
    public void setCoordinateType(CoordinateConverter converter, int coordinateType) {
        switch (coordinateType) {
            case 0:
                converter.from(CoordinateConverter.CoordType.ALIYUN);
                break;
            case 1:
                converter.from(CoordinateConverter.CoordType.BAIDU);
                break;
            case 2:
                converter.from(CoordinateConverter.CoordType.GOOGLE);
                break;
            case 3:
                converter.from(CoordinateConverter.CoordType.GPS);
                break;
            case 4:
                converter.from(CoordinateConverter.CoordType.MAPABC);
                break;
            case 5:
                converter.from(CoordinateConverter.CoordType.MAPBAR);
                break;
            case 6:
                converter.from(CoordinateConverter.CoordType.SOSOMAP);
                break;

        }
    }

    // 求亮点间距离
    public void calculateLineDistance(MethodCall call) {
        DPoint startDP = new DPoint();
        startDP.setLongitude((Double) call.argument("startLng"));
        startDP.setLatitude((Double) call.argument("startLat"));
        DPoint endDP = new DPoint();
        endDP.setLongitude((Double) call.argument("endLng"));
        endDP.setLatitude((Double) call.argument("endLat"));
        float dic = CoordinateConverter.calculateLineDistance(startDP, endDP);
        this.result.success(dic);
    }
}
